package com.yunduansing.core.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.yunduansing.core.commons.DbCommon;

public class DbUtil {
	private static String url="";
	private static String user="sa";
    private static String pwd="123456";
    private static String driver="";
    private static Logger logger;
    private static Connection con;
    private static Statement stmt=null;
    private static PreparedStatement pstmt=null;
    private static Properties p=new Properties();
    public static void init(){
        try {
            logger= LoggerFactory.getLogger(DbUtil.class);
            logger.info("初始化数据库相关属性文件db.properties：DbUtil构造器");
            InputStream fis=DbUtil.class.getClassLoader().getResourceAsStream("db.properties");//new FileInputStream("db.properties");
            p.load(fis);
            driver=p.getProperty("jdbc.driverClass");
            user=p.getProperty("jdbc.user");
            pwd=p.getProperty("jdbc.password");
            url=p.getProperty("jdbc.jdbcUrl");
            logger.info("开始加载驱动：DbUtil构造器");
            Class.forName(driver);

            logger.info("加载驱动成功：DbUtil构造器");

            con= DriverManager.getConnection(url,user,pwd);
        } catch (ClassNotFoundException e) {
            logger.error("没有找到数据库驱动：DbUtil构造器----"+e.getMessage());
            e.printStackTrace();
        } catch (SQLException e) {
            logger.error("数据库连接异常：DbUtil构造器----"+e.getMessage());
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            logger.error("初始化db.properties异常：DbUtil构造器----"+e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            logger.error("加载db.properties异常：DbUtil构造器----"+e.getMessage());
            e.printStackTrace();
        }
    }

	
	public void conn(){
		
	}
	
	public static ResultUtil close(){
		try {
			if(stmt!=null)
				stmt.close();
			if(pstmt!=null)
				pstmt.close();
			con.close();
			return ResultUtil.init(true,"数据库关闭成功");
		} catch (SQLException e) {
			e.printStackTrace();
			return ResultUtil.init(e);
		}
	}
	
	public static int execute(String sql,Map<String,Object> params) throws SQLException {
        pstmt=con.prepareStatement(sql);
        Iterator<String> it=params.keySet().iterator();
        int i=0;
        while (it.hasNext()){
            String key=it.next();
            Object val=params.get(key);
            if(val instanceof Integer)
                pstmt.setInt(++i,(Integer)val);
            else if(val instanceof String)
                pstmt.setString(++i,val.toString());
            else if(val instanceof Long)
                pstmt.setLong(++i,(Long) val);
            else if(val instanceof Boolean)
                pstmt.setBoolean(++i,(Boolean) val);
            else if(val instanceof Short)
                pstmt.setShort(++i,(Short) val);
            else if(val instanceof Float)
                pstmt.setFloat(++i,(Float) val);
            else if(val instanceof Double)
                pstmt.setDouble(++i,(Double) val);
        }
        int re=pstmt.executeUpdate();
        close();
        return re;
    }

	private static void getParams(){
		
	}
	
	/*public DbCommon.GridModel toPager(DbCommon.PagerModel pager){
		DbCommon.GridModel grid=new DbCommon.GridModel();
		return grid;
	}*/
	
	/*public List toList(String sql,Object o) throws SQLException{
		stmt=con.createStatement();
		ResultSet rs=stmt.executeQuery(sql);
		List<o> list=new ArrayList<o>();
		while(rs.next()){
			//t.getClass().newInstance();
		}
		rs.close();
		close();
		return list;
	}*/
	
	public <T> T toSingle(String sql,Map<String,Object> params) throws SQLException{
		pstmt=con.prepareStatement(sql);
        Iterator<String> it=params.keySet().iterator();
        int i=0;
        while (it.hasNext()){
            String key=it.next();
            Object val=params.get(key);
            if(val instanceof Integer)
                pstmt.setInt(++i,(Integer)val);
            else if(val instanceof String)
                pstmt.setString(++i,val.toString());
            else if(val instanceof Long)
                pstmt.setLong(++i,(Long) val);
            else if(val instanceof Boolean)
                pstmt.setBoolean(++i,(Boolean) val);
            else if(val instanceof Short)
                pstmt.setShort(++i,(Short) val);
            else if(val instanceof Float)
                pstmt.setFloat(++i,(Float) val);
            else if(val instanceof Double)
                pstmt.setDouble(++i,(Double) val);
        }
        ResultSet rs=pstmt.executeQuery();
		return null;
	}
	
	public static boolean login(String a,String pwd) throws SQLException{
		init();
		pstmt=con.prepareStatement("select * from sys_account where `account`=? and `pwd`=?");
		pstmt.setString(1, a);
		pstmt.setString(2, pwd);
		ResultSet rs=pstmt.executeQuery();
		boolean flag=rs.next();
		rs.close();
		close();
		return flag;
	}
	
	public void toDataSet(){
		
	}
}
